package jrunner;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;


public class Person implements Observer, Cloneable, Serializable {
	

	/**
	 * 
	 */
//	private static final long serialVersionUID = 1L;
	private String name;
	private int age;
	private int height;
	private double weight;
	private boolean female;
	private boolean male;
	
	//I set this transient because IO Streams cannot be serialized.
	transient private ExportPersonData export;
	
	{
		name = "";
		age = 0;
		height = 0;
		weight = 0.0;
		female = false;
		male = false;
	}
	//Constructor is private. It is not possible
	//to create Person instances. Only through the 
	//factory method.
	private Person(ExportPersonData e){
		export = new ExportPersonObject();
	}

	//Setters and Getters for all fields.
	public String getName(){
		return this.name;
	}
	public int getAge(){
		return this.age;
	}
	public int getHeight(){
		return this.height;
	}
	public double getWeight(){
		return this.weight;
	}
	public boolean getFemale(){
		return this.female;
	}
	public boolean getMale(){
		return this.male;
	}
	public void setName(String n){
		this.name = n;
	}
	public void setAge(int a){
		this.age = a;
	}
	public void setHeight(int h){
		this.height = h;
	}
	public void setWeight(double w){
		this.weight = w;
	}
	public void setFemaleTrue(){
		this.male = false;
		this.female = true;
	}
	public void setMaleTrue(){
		this.female = false;
		this.male = true;
	}
	
	public static Person getPersonInstance(ExportPersonData export){
		return new Person(export);
	}
	@Override
	public String toString(){
		return this.getName();
	}
	
	//The update Method is implemented through java.util.Observer
	//It listens to an Observable class. When the Observer is finished
	//with an action it activates the update method.
	public void update(Observable arg, Object obj) {
		
		PersonGUI in = (PersonGUI) arg;
		
//		Import all person variables from PersonGUI
//		to the Person object.
		this.setName(in.getName());
		this.setAge(in.getAge());
		this.setHeight(in.getHeight());
		this.setWeight(in.getWeight());
		
		if(in.isFemale()){this.setFemaleTrue();}
		else{
			if(in.isMale()){
				this.setMaleTrue();}
			}
		
		Person personData;
		
		try {
			personData = (Person) (this.clone());
			export.exportPersonData(personData);	
		} 
		catch (CloneNotSupportedException e) {e.printStackTrace();}
		

	}
	public static void main(String[] args){
		
		Person p = Person.getPersonInstance(new ExportPersonObject());
		p.setName("Philipp");
		
		System.out.println(p.toString());
		
	}


}
